///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_UTILITY_CDESKTOPFILE_H_
#define _BUOLA_UTILITY_CDESKTOPFILE_H_

#include <buola/buola.h>
#include <unordered_map>
#include <map>

namespace buola {

///\addtogroup utility
///@{

////////////////////////////////////////////////////////////////////////////

/// configuration file

/// Reads and writes to configuration files which are in the format of
/// .desktop files
/// \todo use iterator access
///
////////////////////////////////////////////////////////////////////////////

class CDesktopFile
{
public:
    typedef std::map<std::string,std::string> TGroup;

//! \name constructor and destructor
//@{
    CDesktopFile();
    CDesktopFile(const io::CURI &pURI);
    virtual ~CDesktopFile();
//@}

    ///load from a URI
    void Load(const io::CURI &pURI);

private:
    ///gets entry which best matches the parameters
    std::string *GetBest(const std::string &pGroup,const std::string &pKey);

public:
    ///get value exactly matching group, key and maybe current locale
    bool Get(const std::string &pGroup,const std::string &pKey,std::wstring &pValue,
            const std::wstring &pDefault=std::wstring());
    ///get value exactly matching group, key and maybe current locale
    bool Get(const std::string &pGroup,const std::string &pKey,std::string &pValue,
            const std::string &pDefault=std::string());
    ///get value exactly matching group, key and maybe current locale
    bool Get(const std::string &pGroup,const std::string &pKey,std::vector<std::string> &pValue);

    ///get value semantically converted to tType
    template<typename tType>
    tType GetValue(const std::string &pGroup,const std::string &pKey,tType pDefault)
    {
        std::string *lString=GetBest(pGroup,pKey);
        if(!lString) return pDefault;
        return to_<tType>(*lString);
    }

    ///gets a group as a map
    const TGroup &GetGroup(const std::string &pName);

private:
    ///hash table of all groups
    std::unordered_map<std::string,TGroup*> mGroups;
};

///@}

/*namespace buola*/ }

#endif
